<?php


namespace app\admin\controller;


use app\common\controller\AdminBase;
use think\Db;

class Login extends AdminBase
{
//    登录页面
    public function index(){
//        获取必应每日一图
        $backgroundImg = getBiyingPic();
        $this->assign('backgroundImg',$backgroundImg);
        return view(':login');
    }
//    登录验证
    public function login(){
        $param = input();
        $result = $this->validate($param,[
            'captcha|验证码'=>'require|captcha',
        ]);

        if ($result !== true){
            return json(['code'=>1,'msg'=>$result]);
        }

        $admin = Db::name('admin')->where(['username|nick_name'=>$param['username']])->find();
        if (!empty($admin)){
            if ($admin['state'] == 10){
                if ($param['password'] == encry_code($admin['password'],'DECODE')){
                    session('admin',$admin);
                    saveAdminLog("管理员后台登录：登录成功");
                    return json(['code'=>0,'msg'=>'登录成功']);
                }else{
                    saveAdminLog("管理员后台登录：密码错误");
                    return json(['code'=>1,'msg'=>'密码错误']);
                }
            }else{
                saveAdminLog("管理员后台登录：当前用户已被锁定，无法登录");
                return json(['code'=>1,'msg'=>'当前用户已被锁定，无法登录']);
            }
        }else{
            saveAdminLog("管理员后台登录：当前用户不存在");
            return json(['code'=>1,'msg'=>'当前用户不存在']);
        }
    }
//    退出
    public function login_out(){
        session(null);
        return redirect('login/index');
    }
//    清除缓存
    public function clear_cache(){
        if (delete_dir_file(env('runtime_path').'/temp/') || delete_dir_file(env('runtime_path').'/cache/')) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }
}
